<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermissions(1, '0')">添加权限</el-button>
        </div>
        <!-- row-key：如果数据为 树状，表格渲染可成树状-->
        <el-table border :data="tableData" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="addPermissions(2, scope.row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="hDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹框 -->
      <el-dialog :visible.sync="showDialog" :title="isEdit?'编辑':'新增'" @close="hClose">
        <!-- 表单内容 -->
        <el-form ref="formRef" label-width="100px" :model="formData">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button>取消</el-button>
            <el-button type="primary" @click="doSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { arr2tree } from '@/utils/index'
export default {
  data() {
    return {
      tableData: [],
      showDialog: false,
      formData: {
        name: '',
        description: '',
        code: '',
        enVisible: '0',
        pid: '',
        type: ''
      },
      isEdit: false
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    async getPermission() {
      const res = await getPermissionList()
      // 把数据转换成树状数据
      this.tableData = arr2tree(res.data, '0')
    },
    // 点击添加按钮，页面级添加按钮type=1 pid=0，按钮级添加按钮type=2 pid=scope.row.id
    addPermissions(type, pid) {
      this.isEdit = false
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 添加函数
    async doAdd() {
      await addPermission(this.formData)
      this.getPermission()
      this.showDialog = false
    },
    // 点击确认按钮
    doSubmit() {
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    hClose() {
      // 清空表单
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        pid: '',
        type: ''
      }
      this.$refs.formRef.resetFields()
    },
    // 点击删除
    hDelete(id) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.getDelete(id)
        this.getPermission()
      }).catch(() => {
      })
    },
    // 删除函数
    async getDelete(id) {
      await delPermission(id)
    },
    // 编辑按钮
    async hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      const res = await getPermissionDetail(id)
      this.formData = res.data
    },
    // 编辑函数
    async doEdit() {
      await updatePermission(this.formData)
      this.getPermission()
      this.showDialog = false
    }
  }
}
</script>
